FROM repo.seres.cn/node:22-alpine AS builder
WORKDIR /app

# 设置构建环境变量
ENV NODE_OPTIONS="--max-old-space-size=4096"
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV NPM_CONFIG_REGISTRY=https://repo.seres.cn/nexus/repository/npm/
ENV NPM_CONFIG_STRICT_SSL=false

ARG NODE_ENV="dev"
ARG URL_PATH="mobile"

RUN echo "NODE_ENV=${NODE_ENV}"

# 配置Alpine软件源并添加重试机制
RUN for i in 1 2 3; do \
        echo "Attempt $i: Configuring Alpine repositories..." && \
        echo "https://repo.seres.cn/nexus/repository/alpine/v3.21/main" > /etc/apk/repositories && \
        echo "https://repo.seres.cn/nexus/repository/alpine/v3.21/community" >> /etc/apk/repositories && \
        break || \
        if [ $i -lt 3 ]; then \
            echo "Retry configuring repositories in 5 seconds..." && \
            sleep 5; \
        else \
            echo "Failed to configure repositories after 3 attempts" && \
            exit 1; \
        fi \
    done

# 安装系统依赖并添加重试机制
RUN for i in 1 2 3; do \
        echo "Attempt $i: Updating and installing packages..." && \
        apk update && \
        apk add --no-cache --virtual .build-deps \
            bash \
            curl \
            python3 \
            make \
            g++ \
            git \
            openssl \
            ca-certificates \
            autoconf \
            automake \
            libtool \
            nasm \
            libpng-dev \
            gifsicle \
            vips-dev && \
        rm -rf /var/cache/apk/* && \
        break || \
        if [ $i -lt 3 ]; then \
            echo "Retry package installation in 10 seconds..." && \
            sleep 10; \
        else \
            echo "Failed to install packages after 3 attempts" && \
            # 尝试安装最基本的包，以便继续构建
            echo "Trying to install minimal packages..." && \
            apk update && \
            apk add --no-cache bash curl && \
            echo "Minimal packages installed, continuing with build..." && \
            break; \
        fi \
    done

# 使用npm安装yarn
RUN npm config set registry https://repo.seres.cn/nexus/repository/npm/ \
    && npm config set strict-ssl false \
    && npm install -g yarn@1.22.19 --force \
    && yarn --version \
    && yarn config set registry https://repo.seres.cn/nexus/repository/npm/ \
    && yarn config set sass_binary_site https://repo.seres.cn/nexus/repository/npm/node-sass \
    && yarn config set disturl https://repo.seres.cn/nexus/repository/npm/node \
    && yarn config set network-timeout 600000 \
    && yarn config set enable-network-idle false

# 分阶段安装依赖
COPY package.json yarn.lock ./

# 修改yarn.lock中的URL为内网地址
RUN echo "Replacing URLs in yarn.lock with internal repository URLs..." && \
    sed -i 's|https://registry.npmmirror.com|https://repo.seres.cn/nexus/repository/npm|g' yarn.lock && \
    sed -i 's|https://registry.yarnpkg.com|https://repo.seres.cn/nexus/repository/npm|g' yarn.lock && \
    sed -i 's|https://registry.npmjs.org|https://repo.seres.cn/nexus/repository/npm|g' yarn.lock && \
    sed -i 's|http://registry.npmjs.org|https://repo.seres.cn/nexus/repository/npm|g' yarn.lock && \
    sed -i 's|https://npm.pkg.github.com|https://repo.seres.cn/nexus/repository/npm|g' yarn.lock && \
    sed -i 's|https://npm.fontawesome.com|https://repo.seres.cn/nexus/repository/npm|g' yarn.lock && \
    sed -i 's|https://nodejs.org/download/release|https://repo.seres.cn/nexus/repository/npm/node|g' yarn.lock && \
    sed -i 's|https://github.com|https://repo.seres.cn/nexus/repository/npm|g' yarn.lock && \
    echo "URL replacement in yarn.lock completed."

# 添加重试机制安装依赖
RUN for i in 1 2 3; do \
        echo "Attempt $i: Installing dependencies..." && \
        yarn install \
            --frozen-lockfile \
            --network-timeout 600000 \
            --non-interactive \
            --update-checksums \
            --verbose && break || \
        if [ $i -lt 3 ]; then \
            echo "Retry yarn install in 10 seconds..." && \
            sleep 10; \
        else \
            echo "Failed to install dependencies after 3 attempts" && \
            exit 1; \
        fi \
    done

# 复制项目文件并构建
COPY . .

# 添加重试机制构建项目
RUN for i in 1 2 3; do \
        echo "Attempt $i: Building project..." && \
        yarn build:${NODE_ENV} && break || \
        if [ $i -lt 3 ]; then \
            echo "Retry build in 10 seconds..." && \
            sleep 10; \
        else \
            echo "Build failed after 3 attempts" && \
            exit 1; \
        fi \
    done

# 清理构建依赖（生产镜像不需要）
RUN apk del .build-deps

# 生产阶段
FROM repo.seres.cn/nginx:stable-alpine3.17
COPY --from=builder /app/dist /usr/share/nginx/html

COPY nginx.conf  /etc/nginx/conf.d/default.conf.template  

# 启动脚本（下载远程配置并启动 Nginx）
COPY Start.sh  /Start.sh  

# 设置启动脚本权限并定义入口
RUN chmod +x /Start.sh
ENTRYPOINT ["sh","./Start.sh"]